package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Export;
import cn.itcast.domain.cargo.ExportProduct;
import cn.itcast.domain.cargo.ExportProductExample;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.ExportProductService;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.web.controller.BaseController;
import cn.itcast.web.utils.BeanMapUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import java.io.InputStream;
import java.util.*;

@Controller
@RequestMapping("/cargo/export")
@Slf4j
public class PdfController extends BaseController {
    /**
     * 导出PDF (1) 入门案例
     * http://localhost:8080/cargo/export/exportPdf.do?id=2
     */


    @RequestMapping("/exportPdf1")
    public void exportPdf1() throws Exception {
        //1. 获取jasper文件流
        InputStream in =
                session.getServletContext().getResourceAsStream("/jasper/test01.jasper");

        //2. 创建JasperPrint，用于往模板中填充数据
        // 参数1 pdf文件流
        // 参数2 数据填充的方式：以map方式填充
        // 参数3 数据填充的方式：以数据源的方式填充
        JasperPrint jasperPrint = JasperFillManager.fillReport(in,new HashMap<>(),new JREmptyDataSource());

        //3. 导出PDF
        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
    }


    @RequestMapping("/exportPdf2")
    public void exportPdf2() throws Exception {
        //1. 获取jasper文件流
        InputStream in =
                session.getServletContext().getResourceAsStream("/jasper/test03.jasper");

        //2. 创建JasperPrint，用于往模板中填充数据
        //构造map，map的key必须与模板中的parameters一致
        HashMap<String, Object> map = new HashMap<>();
        map.put("username","老王");
        map.put("email","lw@export.cn");
        map.put("company","传智播客");
        map.put("deptName","Jave教研部");

        //参数1:模板文件输入流；参数2：传递到模板文件中的key-value类型的参数；参数3：数据列表参数
        JasperPrint jasperPrint = JasperFillManager.fillReport(in,map,new JREmptyDataSource());

        //3. 导出PDF
        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
    }

    @RequestMapping("/exportPdf3")
    public void exportPdf3() throws Exception {
        //1. 获取jasper文件流
        InputStream in =
                session.getServletContext().getResourceAsStream("/jasper/test04_jdbc.jasper");

        //2. 创建JasperPrint，用于往模板中填充数据
        ArrayList<User> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            User user = new User();
            user.setUserName("name"+i);
            user.setEmail(i+"qq.com");
            user.setCompanyName("企业"+i);
            user.setDeptName("部门"+i);
            list.add(user);
        }
        //将list构造为jrdatasource
        JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list);

        //参数1:模板文件输入流；参数2：传递到模板文件中的key-value类型的参数；参数3：数据列表参数
        JasperPrint jasperPrint = JasperFillManager.fillReport(in,new HashMap<>(), dataSource);

        //3. 导出PDF
        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
    }

    @RequestMapping("/exportPdf4")
    public void exportPdf4() throws Exception {
        //1. 获取jasper文件流
        InputStream in =
                session.getServletContext().getResourceAsStream("/jasper/test06_group.jasper");

        //2. 创建JasperPrint，用于往模板中填充数据
        ArrayList<User> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {//一个企业的员工搞完，在进行下一个企业
            for (int j = 0; j < 5; j++) {
                User user = new User();
                user.setUserName("name"+j);
                user.setEmail(j+"@qq.com");
                user.setCompanyName("企业"+i);
                user.setDeptName("部门"+j);
                list.add(user);
            }
        }
        //将list构造为jrdatasource
        JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list);

        //参数1:模板文件输入流；参数2：传递到模板文件中的key-value类型的参数；参数3：数据列表参数
        JasperPrint jasperPrint = JasperFillManager.fillReport(in,new HashMap<>(), dataSource);

        //3. 导出PDF
        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
    }


    @RequestMapping("/exportPdf5")
    public void exportPdf5() throws Exception {
        //1. 获取jasper文件流
        InputStream in =
                session.getServletContext().getResourceAsStream("/jasper/test07_chart.jasper");

        //2. 创建JasperPrint，用于往模板中填充数据
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("title","标题"+i);
            map.put("value",new Random().nextInt(100));
            list.add(map);
        }
        //将list构造为jrdatasource
        JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list);

        //参数1:模板文件输入流；参数2：传递到模板文件中的key-value类型的参数；参数3：数据列表参数
        JasperPrint jasperPrint = JasperFillManager.fillReport(in,new HashMap<>(), dataSource);

        //3. 导出PDF
        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
    }


    @Reference
    private ExportService exportService;
    @Reference
    private ExportProductService exportProductService;

    @RequestMapping("/exportPdf")
    public void exportPdf(String id) throws Exception {
        //1. 获取jasper文件流
        InputStream in =
                session.getServletContext().getResourceAsStream("/jasper/export.jasper");

        //第一步：根据报运单id查询报运单
        Export export = exportService.findById(id);
        // 对象转换为map集合（调用工具类）
        Map<String, Object> map = BeanMapUtils.beanToMap(export);

        //第二步：根据报运单id查询商品
        ExportProductExample example = new ExportProductExample();
        example.createCriteria().andExportIdEqualTo(id);
        List<ExportProduct> productList = exportProductService.findAll(example);

        // 构造数据源集合对象
        JRDataSource dataSource = new JRBeanCollectionDataSource(productList);
        //2. 创建JasperPrint，用于往模板中填充数据
        JasperPrint jasperPrint = JasperFillManager.fillReport(in, map, dataSource);

        //3. 导出PDF
        response.setHeader("content-disposition","attachment;fileName=export.pdf");
        ServletOutputStream out = response.getOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint, out);
        out.close();
    }


}
